package com.yks.rpc.transport;

import org.apache.commons.io.IOUtils;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.util.HashMap;

/**
 * @Description 调用远程服务的代理类
 * @Author
 * @Date 2020/4/15 20:19
 **/
public class RemoteInvocationHandler implements InvocationHandler {

    private Class clazz;

    public RemoteInvocationHandler(Class clazz) {
        this.clazz = clazz;
    }

    public Object invoke(Object proxy, Method method, Object[] args) {
        try {
            //调用远程服务
            Peer peer = new Peer();
            peer.setHost("127.0.0.1");
            peer.setPort(3000);
            TransportClient client = new TransportClient();
            client.connect(peer);

            InputStream write = client.write(new ByteArrayInputStream("hello".getBytes()));

            byte[] bytes = IOUtils.readFully(write, write.available());
            return new String(bytes);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }
}
